package com.fiume.travel.config;

/**
 * <p>
 * Description:SpringMVC的核心配置类
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-22 22:41
 * @see com.fiume.travel.config
 */


import com.fiume.travel.interceptor.CrossInterceptor;
import com.fiume.travel.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

@ComponentScan(value = {"com.fiume.travel.web","springfox.documentation.swagger.web"})
@Configuration
public class PlatformSpringMvcConfig extends SpringMvcConfig {

    /**
     * 登录拦截配置
     */
    @Bean("loginInterceptor")
    public LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }

    /**
     * 跨域拦截器
     */
    @Bean("crossInterceptor")
    public CrossInterceptor getCrossInterceptor(){
        return new CrossInterceptor();
    }

    /**
     * 静态资源路径 映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 支持webjars
         */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        /**
         * 支持swagger
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }

    /**
     * 给拦截器设置拦截的路径
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 配置跨域拦截器拦截的路径
        registry.addInterceptor(getCrossInterceptor())
                .addPathPatterns("/**");

        // 登录拦截
        registry.addInterceptor(loginInterceptor())
                // 设置拦截的路径
                .addPathPatterns("/**")
                // 设置排除的路径
                .excludePathPatterns(
                        // 放行user模块下的所有
                        "/**/user/**",
                        // 放行seller模块下的所有
                        "/**/seller/**",
                        // 放行category模块下的所有
                        "/**/category/**",
                        // 放行首页中线路的信息
                        "/**/route/**",
                        //  放行swagger静态资源不拦截
                        "/**/webjars/**",
                        // 放行swagger静态资源不拦截
                        "/**/swagger-ui.html",
                        // 放行swagger静态资源不拦截
                        "/**/swagger-resources/**",
                        // 放行swagger静态资源不拦截
                        "/**/v2/**"
                );
    }
}
